package com.xiaomengkuan.product.controller;

import com.xiaomengkuan.common.entity.vo.PageVo;
import com.xiaomengkuan.common.exception.CasualException;
import com.xiaomengkuan.common.util.DetailSelector;
import com.xiaomengkuan.common.util.R;
import com.xiaomengkuan.product.entity.vo.ProductDetailResponseVo;
import com.xiaomengkuan.product.entity.vo.ProductResponseVo;
import com.xiaomengkuan.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-26 7:01 下午, File Encoding: utf8 --
 */
@Api(tags = "查询商品")
@RestController
@RequestMapping("product/product")
public class ProductController {
    @Resource
    private ProductService productService;

    /**
     * 根据用户ID分页查询首页数据
     *
     * @param requestBody requestBody
     * @return R
     */
    @ApiOperation(value = "根据用户ID分页查询首页数据")
    @PostMapping("getIndex")
    public R getIndexByPageAndUserId(@RequestBody Map<String, Object> requestBody) {
        PageVo<ProductResponseVo> list =
                productService.getIndex(requestBody);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).page(list);
    }

    @ApiOperation(value = "判断商品是否存在")
    @GetMapping("isExist/{id}")
    public R isExist(@PathVariable("id") String id) {
        boolean flag = productService.isExist(id);
        return flag ? R.ok().detail(DetailSelector.EXIST) :
                R.err().detail(DetailSelector.NOT_EXIST);
    }

    /**
     * 查询商品详细
     *
     * @param requestBody requestBody
     * @return R
     */
    @ApiOperation(value = "根据商品id查询商品详细")
    @PostMapping("detail")
    public R getDetailById(@RequestBody Map<String, Object> requestBody) {
        ProductDetailResponseVo detail =
                productService.getDetailById(requestBody);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).putItem(detail);
    }


    /**
     * 查找标签对应的商品列表
     *
     * @param id 标签id
     * @return R
     */
    @ApiOperation(value = "查找标签对应的商品列表")
    @GetMapping("getByTag/{id}/{page}")
    public R getListByTag(@PathVariable("id") String id,
                          @PathVariable("page") int page) {
        List<ProductResponseVo> list =
                productService.getListByTag(id, page);
        if (!StringUtils.isEmpty(list)) {
            return R.ok().detail(DetailSelector.QUERY_SUCCESS).putList(list);
        } else {
            throw new CasualException(5000, DetailSelector.NOTHING_FOUND);
        }
    }


}
